package com.example.fragment

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.example.R
import com.example.bus.AndroidI2cJniControl

class I2cFragment : Fragment() {
    var logList  = "";


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_i2c, container, false)

        val textView =  view.findViewById<TextView>(R.id.log_text)//编辑框

        view.findViewById<Button>(R.id.test_i2c).setOnClickListener {
            val log = AndroidI2cJniControl.test()
            addLog(textView,"test result: $log")
            addLog(textView,"\n")
        }

        view.findViewById<Button>(R.id.inti_device).setOnClickListener {
            AndroidI2cJniControl.initDevice()
            addLog(textView,"初始化设备\n")
        }

        view.findViewById<Button>(R.id.getBME28Value).setOnClickListener {
            val doubleList = AndroidI2cJniControl.getBME28Value()
            addLog(textView,"pressure : ${"%.2f".format(doubleList[0])} hPa ")
            addLog(textView,"temp : ${"%.2f".format(doubleList[1])} C ")
            addLog(textView,"hum : ${"%.2f".format(doubleList[2])}  ")
            addLog(textView,"\n")
        }

        view.findViewById<Button>(R.id.getImuValue).setOnClickListener {
            val doubleList = AndroidI2cJniControl.getImuValue()
            addLog(textView,"Roll : ${"%.2f".format(doubleList[0])} Pitch  : ${"%.2f".format(doubleList[1])}  Yaw  : ${"%.2f".format(doubleList[2])}")
            addLog(textView,"Acceleration : X: ${"%.2f".format(doubleList[3])} Y: ${"%.2f".format(doubleList[4])}  Z: ${"%.2f".format(doubleList[5])}")
            addLog(textView,"Gyroscope : X: ${"%.2f".format(doubleList[6])} Y: ${"%.2f".format(doubleList[7])}  Z: ${"%.2f".format(doubleList[8])}")
            addLog(textView,"Magnetic : X: ${"%.2f".format(doubleList[9])} Y: ${"%.2f".format(doubleList[10])}  Z: ${"%.2f".format(doubleList[11])}")
            addLog(textView,"\n")
        }
        return view
    }

    private fun  addLog (textView:TextView,log:String ){
        logList +=(log+"\n");
        textView.text=logList
    }
}